package com.ums.upos.service;

import android.os.Bundle;
import android.os.RemoteException;

import com.nexgo.oaf.apiv3.SdkResult;
import com.nexgo.oaf.apiv3.device.reader.CardInfoEntity;
import com.nexgo.oaf.apiv3.device.reader.CardReader;
import com.nexgo.oaf.apiv3.device.reader.CardSlotTypeEnum;
import com.nexgo.oaf.apiv3.device.reader.OnCardInfoListener;
import com.ums.upos.GlobalDataHolder;
import com.ums.upos.uapi.ServiceResult;
import com.ums.upos.uapi.device.reader.mag.MagCardInfoEntity;
import com.ums.upos.uapi.device.reader.mag.MagCardReader;
import com.ums.upos.uapi.device.reader.mag.OnSearchMagCardListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;

/**
 * Created by xiaox on 15-10-7.
 */
public class MagCardReaderBinder extends MagCardReader.Stub {
    private final String TAG = "MagCardReaderService";
    private Logger log;
    CardReader sdkCardReader;

    MagCardReaderBinder() {
        log = LoggerFactory.getLogger(TAG);
        sdkCardReader = GlobalDataHolder.getInstance().getDeviceEngine().getCardReader();
    }

    @Override
    public int searchCard(final OnSearchMagCardListener listener, int timeout) throws RemoteException {
        try {
            log.info("call searchCard Mag, timeout:{}", timeout);
            if (listener == null) return ServiceResult.Param_In_Invalid;
            HashSet<CardSlotTypeEnum> cardSlotTypeSet = new HashSet<>();
            cardSlotTypeSet.add(CardSlotTypeEnum.SWIPE);
            int result = sdkCardReader.searchCard(cardSlotTypeSet, timeout, new OnCardInfoListener() {
                @Override
                public void onCardInfo(int i, CardInfoEntity cardInfoEntity) {
                    try {
                        if (i == SdkResult.Success) {
                            MagCardInfoEntity mcie = new MagCardInfoEntity();
                            mcie.setCardNo(cardInfoEntity.getCardNo());
                            mcie.setTk1(cardInfoEntity.getTk1());
                            String tk2 = cardInfoEntity.getTk2();
                            //mhl20171031 兼容旧版机器
                            if (tk2 != null) {
                                try {
                                    tk2 = tk2.replace('D', '=');
                                    if (tk2.charAt(tk2.length() - 1) == 'F')
                                        tk2 = tk2.substring(0, tk2.length() - 1);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    tk2 = cardInfoEntity.getTk2();
                                }
                            }
                            mcie.setTk2(tk2);
//                            mcie.setTk2(cardInfoEntity.getTk2());
                            mcie.setTk3(cardInfoEntity.getTk3());
                            mcie.setTk1ValidResult(0);
                            mcie.setTk2ValidResult(0);
                            mcie.setTk3ValidResult(0);
                            listener.onSearchResult(ServiceResult.Success, mcie);
                        } else {
                            listener.onSearchResult(i == SdkResult.TimeOut ? ServiceResult.TimeOut : ServiceResult.MagCardReader_Other_Error, null);
                        }
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onSwipeIncorrect() {

                }

                @Override
                public void onMultipleCards() {

                }
            });
            if (result == SdkResult.Success) {
                return ServiceResult.Success;
            } else {
                return ServiceResult.Fail;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ServiceResult.Fail;
        }
    }

    @Override
    public void stopSearch() throws RemoteException {
        log.info("call stopSearch");
        try {
            sdkCardReader.stopSearch();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setIsCheckLrc(boolean isCheckLrc) throws RemoteException {
        // FIXME: 16/9/19 no support
    }

    @Override
    public int searchCardEx(OnSearchMagCardListener listener, int timeout, Bundle data) throws RemoteException {
        return 0;
    }
}
